#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <mypushbutton.h>
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include "ChooseLevelScene.h"
#include <QSoundEffect>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setFixedSize(320,588);

    setWindowIcon(QIcon(":/res/Coin0001.png"));

    setWindowTitle("翻金币主场景");

    // 绑定退出按钮
    connect(ui->actionQuit,&QAction::triggered,this,[=](){
        this->close();
    });

    // 设置开始按钮
    MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png","");
    startBtn->setParent(this);
    startBtn->move((this->width() - startBtn->width())*0.5,this->height()*0.7);

    chooseScene = new ChooseLevelScene;

    // 监听选择关卡页面返回信息
    connect(chooseScene,&ChooseLevelScene::chooseBack,this,[=](){
        QTimer::singleShot(200,this,[=](){
            this->setGeometry(chooseScene->geometry());
            chooseScene->hide();
            this->show();
        });
    });

    // 点击开始的背景音乐
    QSoundEffect *sound = new QSoundEffect(this);
    sound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    connect(startBtn,&MyPushButton::clicked,this,[=](){
        sound->play();
        // 跳动动画
        startBtn->zoomDown();
        startBtn->zoomUp();
        // 延时展示选关页面
        QTimer::singleShot(500,this,[=](){
            chooseScene->setGeometry(this->geometry());
            this->hide();
            chooseScene->show();
        });
    });



}

void MainWindow::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(20,30,pix);
}

MainWindow::~MainWindow()
{
    delete ui;
}
